<?php
/**
 * Created by <何不秉烛游 www.hbbzy.me>.
 * User: hbbzy
 * DateTime: 2018/3/16 16:29
 * Test.php
 */

namespace app\backend\controller;

use think\Db;
class Test extends Base
{
    public function index()
    {
        $database = $this->request->get('database');

        if(empty($database)){
            exit('database not exist!');
        }

        $param = [$database];
        $table = Db::query('SELECT TABLE_NAME, `ENGINE`, TABLE_COLLATION,TABLE_COMMENT FROM information_schema.`TABLES` WHERE TABLE_SCHEMA = ?',$param);

        $record = array();
        foreach($table as $k=>$t){
            $param[1] = $t['TABLE_NAME'];
            $cols = Db::query('SELECT COLUMN_NAME,COLUMN_TYPE,COLUMN_DEFAULT,IS_NULLABLE,CHARACTER_SET_NAME,COLLATION_NAME,IF (EXTRA = \'auto_increment\',\'是\',\'\') \'IS_AUTO_INCREMENT\',COLUMN_COMMENT,IF (COLUMN_KEY = \'PRI\', \'是\', \'\') \'IS_PRI\' FROM information_schema.COLUMNS WHERE TABLE_SCHEMA =? AND TABLE_NAME = ?',$param);
            $record[$k] = $t;
            $record[$k]['cols'] = $cols;

            $stas = Db::query('SELECT INDEX_NAME, GROUP_CONCAT(COLUMN_NAME) COLUMN_NAME, IF ( NON_UNIQUE = 0 AND INDEX_NAME = \'PRIMARY\' ,\'主键索引\' ,IF( INDEX_TYPE =\'FULLTEXT\' ,\'全文索引\' ,IF( NON_UNIQUE = 1 AND locate(\',\',GROUP_CONCAT(COLUMN_NAME)) > 0 ,\'多列索引\' ,\'普通索引\'))) `INDEX` FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = ? AND TABLE_NAME=? GROUP BY TABLE_NAME, `INDEX_NAME` DESC ORDER BY TABLE_NAME',$param);
            $record[$k]['index'] =  $stas;

        }
        $this->assign('database',$database);
        $this->assign('create_time',date('Y-m-d H:i:s',time()));
        $this->assign('record',$record);

        return $this->fetch();
        //return $this->fetch();
    }
}